const app = getApp();
Page({
  data: {
    recordList: [],
    page: 1,
    canReachBottom: true,
    op: '',
  },
  onLoad(){
    this.getBaseInfo();
    this.getRecordList();
  },
  getRecordList() {
    let { page, recordList, recept_person, control_title  } = this.data;
    const isAdmin = JSON.parse(this.options.isAdmin);
    wx.setNavigationBarTitle({ title: isAdmin ? `${recept_person}记录` : `${control_title}记录` });
    let op = isAdmin ? 'resolveAlarmRecord' : 'alarmRecord';
    app.util.request({
      url: 'entry/wxapp/record',
      data: {
        op,
        page,
      },
      module: 'cm_video_alarm',
      success: res => {
        if (!res.data.data || res.data.data.length <= 0) {
          this.setData({
            canReachBottom: false,
          });
          return;
        }
        recordList = [...recordList, ...res.data.data];
        this.setData({
          isAdmin,
          recordList,
        });
      },
    });
  },
  toAlarmPlace(e) {
    let { address = '', lat = 0, lng = 0, page } = e.currentTarget.dataset;
    let url = `${page}?address=${address}&lat=${lat}&lng=${lng}`;
    if (!address) {
      wx.showModal({
        title: '温馨提示',
        content: '暂无地址',
      });
      return;
    }
    app.util.navigateTo(url);
  },
  onReachBottom() {
    let { canReachBottom, page } = this.data;
    if (canReachBottom) {
      page++;
      this.setData({ page });
      this.getRecordList();
    } else {
      this.setData({
        canReachBottom: false,
      });
    }
  },
  getBaseInfo(){
    let { recept_person = '', control_title = '' } = wx.getStorageSync('alarmBaseInfo');
    this.setData({
      recept_person,
      control_title,
    });
  },
  playVideo(e) {
    let { videoType, recordId } = e.currentTarget.dataset;
    let { recordList } = this.data;
    let currentRecord = recordList.find(item => item.id === recordId);
    this.setData({
      currentVideo: currentRecord ? videoType === 'alarm' ? currentRecord.user_video : currentRecord.police_video : '',
    });
  },
});
